package com.baiyang.server.control.system;

import java.util.List;
import java.util.Map;

import com.baiyang.server.model.system.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.model.system.MenuRole;
import com.baiyang.server.service.system.MenuRoleService;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/menuRoleOperation")
@Jurisdiction(value = "角色菜单关联表")
public class MenuRoleCon {
	@Autowired
	private MenuRoleService menuRoleService = null;
	
	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "批量读取")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<MenuRole> getPage(@RequestBody JSONObject object){
		Letter<MenuRole> letter = new Letter<MenuRole>();
		try {
			PageEntity<MenuRole> result = menuRoleService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根据条件获取menuid集合，不包含父菜单
	 *
	 * @return
	 */
	@Jurisdiction(value = "selectListExParent")
	@RequestMapping(value = "/selectListExParent", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> selectListExParent(@RequestBody MenuRole menuRole) {
		Letter<String> letter = new Letter<String>();
		try {
			List<String> result = menuRoleService.selectListExParent(menuRole);
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件获取数据
	 * @return
	 */
	@RequestMapping(value = "/selectList",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<MenuRole> selectList(@RequestBody MenuRole menuRole){
		Letter<MenuRole> letter = new Letter<MenuRole>();
		try {
			
			List<MenuRole> result = menuRoleService.selectList(menuRole);
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 根据条件获取一个
	 * @param menuRole
	 * @return
	 */
	@Jurisdiction(value = "读取")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<MenuRole> selectOne(@RequestBody MenuRole menuRole){
		Letter<MenuRole> letter = new Letter<MenuRole>();
		try {
			MenuRole obj = menuRoleService.selectOne(menuRole);
			letter.setData(obj);
			letter.setDetail("数据获取成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}
	
	/**
	 * 插入数据
	 * @param menuRole
	 * @return
	 */
	@Jurisdiction(value = "新增")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody MenuRole menuRole){
		Letter<String> letter = new Letter<String>();
		menuRole.setId(DataTool.createUUID());
		Map<String, String> sub = SubjectNotNull.subject(menuRole);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！" + sub.toString());
			return letter;
		}
		try {
			menuRoleService.insert(menuRole);
			letter.setDetail("数据插入成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 批量插入数据
	 *
	 * @param object
	 * @return
	 */
	@Jurisdiction(value = "insertBatch")
	@RequestMapping(value = "/insertBatch", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody JSONObject object) {
		Letter<String> letter = new Letter<String>();
		try {
			List<MenuRole> menuRoles = object.getJSONArray("list").toJavaList(MenuRole.class);
			String roleId = object.getString("roleId");
			menuRoleService.DelAndInsertBatch(menuRoles, roleId);
			letter.setResult("SUCCESS");
			letter.setDetail("数据插入成功！");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝条件进行删除
	 * @param menuRole
	 * @return
	 */
	@Jurisdiction(value = "删除")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody MenuRole menuRole){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(menuRole)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			menuRoleService.delete(menuRole);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 *
	 * @param menuRoles
	 * @return
	 */
	@Jurisdiction(value = "deletes")
	@RequestMapping(value = "/deletes", produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> deletes(@RequestBody List<MenuRole> menuRoles) {
		Letter<String> letter = new Letter<String>();
		if (DataTool.isBlank(menuRoles)) {
			letter.setDetail("请选中至少一行！");
			letter.setResult("FAIL");
			return letter;
		}
		try {
			menuRoleService.deletes(menuRoles);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		} catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}
	
	/**
	 * 根绝id更新
	 * @param menuRole
	 * @return
	 */
	@Jurisdiction(value = "修改")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody MenuRole menuRole){
		Letter<String> letter = new Letter<String>();
		if(menuRole.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			menuRoleService.updateById(menuRole);
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败！");
		}
		return letter;	
	}
}
